/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.study.java.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * NIO 服务端
 * @author flan
 * @date   2015年12月11日
 */
public class NIOServer {
	
	private Selector selector;
	
	/**
	 *  获得一个ServerSocket通道，并对该通道做一些初始化的工作
	 * @param port		服务端绑定的端口号
	 * @throws IOException
	 */
	public void initServer(int port) throws IOException{
		
		// 获得一个ServerSocket通道 
		ServerSocketChannel serverChannel = ServerSocketChannel.open();
		// 设置通道为非阻塞
		serverChannel.configureBlocking(false);
		// 将该通道对应的ServerSocket绑定到port端口  
		serverChannel.socket().bind(new InetSocketAddress(port));
		// 获得一个通道管理器
		selector = Selector.open();
		//将通道管理器和该通道绑定，并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后，  
        //当该事件到达时，selector.select()会返回，如果该事件没到达selector.select()会一直阻塞。
		serverChannel.register(selector, SelectionKey.OP_ACCEPT);
		
	}
	
	/**
	 * 采用轮询的方式监听selector上是否有需要处理的事件，如果有，则进行处理 
	 * @throws IOException
	 */
	public void listen() throws IOException{
		System.out.println("服务端启动成功...");
		
		while(true){
			//当注册的事件到达时，方法返回；否则,该方法会一直阻塞
			selector.select();
			Iterator<SelectionKey> iterator = this.selector.selectedKeys().iterator();
			while (iterator.hasNext()) {
				SelectionKey key = (SelectionKey) iterator.next();
				// 删除已选的key,以防重复处理
				iterator.remove();
				
				// 客户端请求连接事件
				if(key.isAcceptable()){
					ServerSocketChannel server = (ServerSocketChannel) key.channel();
					// 获得和客户端连接的通道
					SocketChannel channel = server.accept();
					// 设置成非阻塞
					channel.configureBlocking(false);
					
					//在这里可以给客户端发送信息
					channel.write(ByteBuffer.wrap("您已连接成功！".getBytes()));
					
					//在和客户端连接成功之后，为了可以接收到客户端的信息，需要给通道设置读的权限。
					channel.register(selector, SelectionKey.OP_READ);
					
				}else if (key.isReadable()){// 获得了可读的事件 
					read(key); 
				}
			}
		}
	}
	
	public void read(SelectionKey key) throws IOException{
		// 服务器可读取消息:得到事件发生的Socket通道
		SocketChannel channel = (SocketChannel) key.channel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		channel.read(buffer);
		
		String msg = new String(buffer.array());
		System.out.println("服务端 ："+msg);
		
	}
	
	public static void main(String[] args) throws IOException {
		NIOServer server = new NIOServer();
		server.initServer(8888);
		server.listen();
	}
	
}












